package cn.xopencode.oss.manager.passport;
import cn.xopencode.common.framework.vo.CommonResult;
import cn.xopencode.oss.convert.passport.PassportConvert;
import cn.xopencode.oss.enums.sms.UserSmsSceneEnum;
import cn.xopencode.oss.manager.sms.UserSmsCodeManager;
import cn.xopencode.oss.rpc.oauth.dto.OAuth2CreateAccessTokenReqDTO;
import cn.xopencode.oss.rpc.oauth.dto.OAuth2RefreshAccessTokenReqDTO;
import cn.xopencode.oss.rpc.passport.vo.*;
import cn.xopencode.oss.service.oauth.OAuth2Service;
import cn.xopencode.userservice.rpc.user.UserRpc;
import cn.xopencode.userservice.rpc.user.dto.UserDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class PassportManager {
    @Autowired
    private UserSmsCodeManager userSmsCodeManager;
    @DubboReference(version = "${dubbo.consumer.UserRpc.version}", validation = "false")
    private UserRpc userRpc;
    @Autowired
    private OAuth2Service oAuth2Service;

    /**
     * 使用手机号与登录密码登录
     * @param loginByMobileDTO
     * @param ip
     * @return
     */
    public PassportAccessTokenRespVO loginByMobile(PassportLoginByMobileReqVO loginByMobileDTO, String ip){
        // 根据用户手机号和登录密码获得用户信息
        CommonResult<UserDTO> createUserResult = userRpc.createUserIfAbsent(
                PassportConvert.INSTANCE.convert03(loginByMobileDTO));
        createUserResult.checkError();
        // 创建访问令牌

        // 返回
        return PassportConvert.INSTANCE.convert( oAuth2Service.createAccessToken(
                new OAuth2CreateAccessTokenReqDTO().setUserId(createUserResult.getData().getId())
                        .setCreateIp(ip)));
    }
    /**
     * 使用手机号与登录密码登录
     * @param loginByMobileDTO
     * @param ip
     * @return
     */
    public PassportAccessTokenRespVO loginByUserName(PassportLoginByUserNameReqVO loginByMobileDTO, String ip){
        // 根据用户手机号和登录密码获得用户信息
        CommonResult<UserDTO> createUserResult = userRpc.createUserIfAbsent(
                PassportConvert.INSTANCE.convert04(loginByMobileDTO));
        createUserResult.checkError();
        // 创建访问令牌


        // 返回
        return PassportConvert.INSTANCE.convert( oAuth2Service.createAccessToken(
                new OAuth2CreateAccessTokenReqDTO().setUserId(createUserResult.getData().getId())
                        .setCreateIp(ip)));
    }

    /**
     * 使用手机号与验证码登录
     * @param loginBySmsDTO
     * @param ip
     * @return
     */
    public PassportAccessTokenRespVO loginBySms(PassportLoginBySmsReqVO loginBySmsDTO, String ip) {
        // 校验验证码
        userSmsCodeManager.verifySmsCode(PassportConvert.INSTANCE.convert(loginBySmsDTO).setScene(UserSmsSceneEnum.LOGIN_BY_SMS.getValue()).setUsedIp(ip));//
        // 获得用户
        CommonResult<UserDTO> createUserResult = userRpc.createUserIfAbsent(
                PassportConvert.INSTANCE.convert02(loginBySmsDTO));
        createUserResult.checkError();
        // 创建访问令牌


        // 返回
        return PassportConvert.INSTANCE.convert( oAuth2Service.createAccessToken(
                new OAuth2CreateAccessTokenReqDTO().setUserId(createUserResult.getData().getId())
                        .setCreateIp(ip)));
    }

    public void sendSmsCode(PassportSendSmsRespVO sendSmsCodeDTO, String ip) {
        userSmsCodeManager.sendSmsCode(
                PassportConvert.INSTANCE.convert(sendSmsCodeDTO).setIp(ip));
    }

    public PassportAccessTokenRespVO refreshToken(String refreshToken, String ip) {
        return PassportConvert.INSTANCE.convert(oAuth2Service.refreshAccessToken(
                new OAuth2RefreshAccessTokenReqDTO().setRefreshToken(refreshToken).setCreateIp(ip)));
    }

}
